package controller;

import java.io.IOException;
import java.util.ArrayList;

import DAO.DishDAO;
import DAO.RecipeDAO;
import DTO.RecipeDTO;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import constants.StringConstants;

/**
 * Servlet implementation class RecipeServlet
 */
public class RecipeServlet extends HttpServlet {
	private static final String PICK_DISH_PAGE = "admin/recipe-pick-dish.jsp";
	private static final String RECIPE_PAGE = "admin/recipe.jsp";
	private static final String RECIPE_SERVLET = "RecipeServlet";

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public RecipeServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		if (request.getSession().getAttribute("ADMIN_USERNAME") != null) {
			String strId = request.getParameter("id") == null ? "" : request
					.getParameter("id").toString();
			String dishList = request.getParameter("dishList") == null ? ""
					: request.getParameter("dishList").toString();

			if (!strId.equals("")) {
				int id = Integer.parseInt(strId);
				RecipeDAO recipeDao = new RecipeDAO();
				RecipeDTO recipe = new RecipeDTO(id, "");
				recipeDao.get(recipe);
				recipeDao.getDishList(recipe);

				DishDAO dishDao = new DishDAO();

				request.setAttribute("RECIPEDTO", recipe);
				request.setAttribute("DISHLIST", dishDao.select());
				RequestDispatcher rd = request
						.getRequestDispatcher(PICK_DISH_PAGE);
				rd.forward(request, response);
			} else if (!dishList.equals("")) {
				System.out.println("i'm in");
			} else {
				RecipeDAO dao = new RecipeDAO();
				ArrayList<RecipeDTO> list = dao.select();

				request.setAttribute("RECIPELIST", list);
				RequestDispatcher rd = request
						.getRequestDispatcher(RECIPE_PAGE);
				rd.forward(request, response);
			}
		} else {
			RequestDispatcher rd = request
					.getRequestDispatcher("admin/admin-login.html");
			rd.forward(request, response);
		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		if (request.getSession().getAttribute("ADMIN_USERNAME") != null) {
			request.setCharacterEncoding("UTF-8");
			String action = request.getParameter("action");
			if (action.equals("add")) {
				String name = request.getParameter("name");
				RecipeDTO recipe = new RecipeDTO(0, name);
				RecipeDAO dao = new RecipeDAO();
				dao.add(recipe);

				response.sendRedirect(RECIPE_SERVLET);
			} else if (action.equals(StringConstants.EDIT)) {
				int id = Integer.parseInt(request.getParameter("id"));
				String name = request.getParameter("name");
				String dishs_str = request.getParameter("dishs_str");
				String[] selectedDishs_id = dishs_str.split(",");
				ArrayList<Integer> selectedDishs = new ArrayList<Integer>();
				if (selectedDishs_id.length != 0) {
					for (String dishId_str : selectedDishs_id) {
						if (dishId_str != "") {
							selectedDishs.add(Integer.parseInt(dishId_str));
						}
					}
				}

				RecipeDTO recipe = new RecipeDTO(id, name);
				RecipeDAO dao = new RecipeDAO();

				dao.addRecipeDish(selectedDishs, recipe.getId());
				dao.update(recipe);

				response.sendRedirect(RECIPE_SERVLET);
			} else if (action.equals(StringConstants.DELETE)) {
				int id = Integer.parseInt(request.getParameter("id"));
				RecipeDAO dao = new RecipeDAO();
				dao.delete(id);

				response.sendRedirect(RECIPE_SERVLET);
			}
		} else {
			RequestDispatcher rd = request
					.getRequestDispatcher("admin/admin-login.html");
			rd.forward(request, response);
		}

	}
}
